﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Flexibles.CApCableFixedAttachmentControl
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.Flexibles;

public class CApCableFixedAttachmentControl : 
  UserControl,
  ICUiFLEXAttachmentEditControl,
  IComponentConnector
{
  internal Border FixedAttachmentBorder;
  internal Canvas CanvasFixedAttachment;
  internal Path CableSimulatedSectionFixed;
  internal Canvas EndFrameAxesFixed;
  internal Canvas EndFramePointerFixed;
  internal TextBlock EndFrameLabelFixed;
  internal Canvas StartFrameAxesFixed;
  internal Canvas StartFramePointerFixed;
  internal TextBlock StartFrameLabelFixed;
  internal Canvas AttachmentFrameAxesFixed;
  internal Canvas AttachmentFramePointerFixed;
  internal TextBlock AttachmentFrameLabelFixed;
  internal Path CableNotSimulatedSectionFixed;
  internal Path DistanceFromStartFocusFixed;
  internal Path DistanceFromStartPathFixed;
  internal Canvas DistanceFromStartPointerFixed;
  internal TextBlock DistanceFromStartLabelFixed;
  internal Rectangle AttachmentGraphicObjectFixed;
  internal Canvas DistanceFromStartHitBoxFixed;
  internal Canvas AttachmentFrameHitBoxFixed;
  internal StackPanel FixedAttachmentPanel;
  internal Grid FixedAttachmentGrid;
  internal TextBlock FixedAttachmentFrameLabel;
  internal TxObjEditBoxControl FixedAttachmentFrameBoxCtrl;
  internal TextBlock FixedDistanceFromStartLabel;
  internal TxNumericEditBoxControl FixedDistanceFromStartTxNumericEditBox;
  private bool _contentLoaded;

  public event ParameterChangeEventHandler ParameterChanged;

  public new string Name { get; set; }

  public CApCableAttachmentStatusAnalysis CableAttachmentStatusAnalysis { get; set; }

  public TxObjEditBoxControl FrameEditBoxCtrlAttachment => this.FixedAttachmentFrameBoxCtrl;

  public ICApFLEXCableAttributeViewModel AttributeViewModel { get; private set; }

  public CApFLEXCableAttachmentViewModel AttachmentViewModel { get; private set; }

  public CApFLEXAttachmentEditorViewModel EditorViewModel { get; }

  public List<Storyboard> AttachmentStoryboards
  {
    get
    {
      return new List<Storyboard>(1)
      {
        this.CanvasFixedAttachment.FindResource((object) "PreviewFixedAttachment") as Storyboard
      };
    }
  }

  public Storyboard ActiveAttachmentStoryboard
  {
    get => this.CanvasFixedAttachment.FindResource((object) "PreviewFixedAttachment") as Storyboard;
  }

  public Shape SimulatedShape => (Shape) this.CableSimulatedSectionFixed;

  private CApCableAttachmentFrameController AttachmentFrameController { get; set; }

  private bool IsControlLoading => this.EditorViewModel.IsControlLoading;

  private bool IsControlActivated => this.EditorViewModel.IsControlActivated;

  private TxNumericEditBoxControl LastFocusedNumericBox { get; set; }

  public CApCableFixedAttachmentControl()
  {
    this.EditorViewModel = new CApFLEXAttachmentEditorViewModel();
  }

  public void InitControl(
    ICApFLEXCableAttributeViewModel attributeViewModel,
    CApFLEXCableAttachmentViewModel attachmentViewModel,
    CApFLEXFrameValidator frameValidator,
    bool isCreationMode,
    CApFlexParameterController parameterController,
    CApCableAttachmentStatusAnalysis cableAttachmentStatusAnalysis,
    CApCableAttachmentFrameController attachmentFrameController)
  {
    this.AttributeViewModel = attributeViewModel;
    this.AttachmentViewModel = attachmentViewModel;
    this.CableAttachmentStatusAnalysis = cableAttachmentStatusAnalysis;
    this.AttachmentFrameController = attachmentFrameController;
    this.InitializeComponent();
    this.RegisterNumericalControlEvents();
    this.InitFixedFrameEditBox(frameValidator, isCreationMode);
    this.UpdateLabelText();
    this.SetBindings();
  }

  private void InitFixedFrameEditBox(CApFLEXFrameValidator frameValidator, bool isCreationMode)
  {
    ((UIElement) this.FixedAttachmentFrameBoxCtrl).Focusable = this.AttachmentViewModel.IsEditableAttachment;
    this.FixedAttachmentFrameBoxCtrl.ValidatorType = (TxValidatorType) 6;
    this.FixedAttachmentFrameBoxCtrl.SetValidator((ITxValidator) frameValidator);
    this.FixedAttachmentFrameBoxCtrl.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.AttachmentFrameEditBoxCtrl_Picked);
    if (!isCreationMode)
      this.FixedAttachmentFrameBoxCtrl.Object = (ITxObject) this.AttachmentViewModel.FixedFrame;
    ((UIElement) this.FixedAttachmentFrameBoxCtrl).KeyUp += new KeyEventHandler(this.FixedAttachmentFrame_KeyUp);
  }

  public void SetUpStoryboardsParameters()
  {
    this.ActiveAttachmentStoryboard.SpeedRatio = 1.5;
    this.ActiveAttachmentStoryboard.AutoReverse = true;
  }

  public void UpdateLabelText() => this.UpdateLinearUnitText();

  public void UpdateLinearUnitText()
  {
    this.FixedDistanceFromStartLabel.Text = CApFLEXStringTable.AttachmentLengthLabel + CApCableUtility.LinearUnitLabelText();
  }

  public void UpdateAttachmentCtrlBox()
  {
    this.FixedAttachmentFrameBoxCtrl.Text = "";
    this.FixedAttachmentFrameBoxCtrl.Object = (ITxObject) this.AttachmentViewModel.FixedFrame;
    if (this.AttachmentViewModel.FixedFrame != null)
      return;
    this.FixedAttachmentFrameBoxCtrl.Focus();
  }

  private void RegisterNumericalControlEvents()
  {
    this.FixedDistanceFromStartTxNumericEditBox.TentativeValueChanged += new TxNumericEditBoxCtrl_TentativeValueChangedEventHandler(this.FixedDistanceFromStartTxNumericEditBox_TentativeValueChanged);
  }

  private void UnregisterNumericalControlEvents()
  {
    this.FixedDistanceFromStartTxNumericEditBox.TentativeValueChanged -= new TxNumericEditBoxCtrl_TentativeValueChangedEventHandler(this.FixedDistanceFromStartTxNumericEditBox_TentativeValueChanged);
  }

  public void UpdateAttachmentParametersWithParameters()
  {
    if (!this.IsControlActivated)
      return;
    this.AttachmentViewModel.Location = CApFLEXAttachmentLocation.AlongCable;
    this.AttachmentViewModel.FixedFrame = (ITxLocatableObject) null;
    this.AttachmentViewModel.SecondaryFixedFrame = (ITxLocatableObject) null;
    this.AttachmentViewModel.LengthFromStart = this.AttributeViewModel.Length * 0.5;
  }

  public void UpdateAttachmentParametersWithAttachment(CApFLEXAttachment attachment)
  {
    if (!this.IsControlActivated)
      return;
    this.AttachmentViewModel.AttachmentType = attachment.AttachmentType;
    this.AttachmentViewModel.Location = attachment.Location;
    this.AttachmentViewModel.FixedFrame = attachment.FixedFrame;
    this.AttachmentViewModel.SecondaryFixedFrame = (ITxLocatableObject) null;
    this.AttachmentViewModel.LengthFromStart = attachment.LengthFromStart;
  }

  public void SetDefaultFocus()
  {
    if (!this.IsControlActivated)
      return;
    this.Dispatcher.BeginInvoke(DispatcherPriority.Render, (Delegate) (() => this.FixedAttachmentFrameBoxCtrl.Focus()));
    this.DoSetDefaultFocus();
  }

  private void DoSetDefaultFocus()
  {
    if (this.FixedAttachmentFrameBoxCtrl.Text != "")
      CApCableUtility.HighlightObject((ITxDisplayableObject) (this.AttachmentViewModel.FixedFrame as TxFrame));
    this.AttachmentViewModel.AttachmentFrameSelected = true;
  }

  public void SetEnabledAllParametersControls(bool isEnabled)
  {
    if (!isEnabled)
      this.LastFocusedNumericBox = this.GetCurrentlyFocusedNumericBox();
    this.FixedAttachmentPanel.IsEnabled = isEnabled;
    if (!isEnabled || this.LastFocusedNumericBox == null)
      return;
    ((UIElement) this.LastFocusedNumericBox).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    this.LastFocusedNumericBox = (TxNumericEditBoxControl) null;
  }

  private void SetBindings()
  {
    Binding binding = new Binding()
    {
      Source = (object) this,
      Path = new PropertyPath("EditorViewModel.IsPreviewIdle", Array.Empty<object>()),
      Converter = (IValueConverter) new BooleanToDiagramVisibilityConverter()
    };
    this.StartFramePointerFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.StartFrameAxesFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.StartFrameLabelFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.EndFramePointerFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.EndFrameAxesFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.EndFrameLabelFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.AttachmentFramePointerFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.AttachmentFrameAxesFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.AttachmentFrameLabelFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.AttachmentFrameHitBoxFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.DistanceFromStartPathFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.DistanceFromStartPointerFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.DistanceFromStartLabelFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.DistanceFromStartFocusFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
    this.DistanceFromStartHitBoxFixed.SetBinding(UIElement.VisibilityProperty, (BindingBase) binding);
  }

  private TxNumericEditBoxControl GetCurrentlyFocusedNumericBox()
  {
    TxNumericEditBoxControl focusedNumericBox = (TxNumericEditBoxControl) null;
    if (this.AttachmentViewModel.DistanceFromStartSelected)
      focusedNumericBox = this.FixedDistanceFromStartTxNumericEditBox;
    return focusedNumericBox;
  }

  public void DidDialogClose()
  {
    this.UnregisterNumericalControlEvents();
    this.FixedAttachmentFrameBoxCtrl.Picked -= new TxObjEditBoxCtrl_PickedEventHandler(this.AttachmentFrameEditBoxCtrl_Picked);
    ((UIElement) this.FixedAttachmentFrameBoxCtrl).KeyUp -= new KeyEventHandler(this.FixedAttachmentFrame_KeyUp);
    this.FixedDistanceFromStartTxNumericEditBox = (TxNumericEditBoxControl) null;
  }

  private void AttachmentFrame_DiagramFocus(object sender, MouseButtonEventArgs e)
  {
    this.FixedAttachmentFrameBoxCtrl.Focus();
  }

  private void AttachmentFrame_GotFocus(object sender, RoutedEventArgs e)
  {
    this.DoSetDefaultFocus();
  }

  private void AttachmentFrame_LostFocus(object sender, RoutedEventArgs e)
  {
    CApCableUtility.UnHighlightObject((ITxDisplayableObject) (this.AttachmentViewModel.FixedFrame as TxFrame));
    this.AttachmentViewModel.AttachmentFrameSelected = false;
  }

  private void DistanceFromStart_DiagramFocus(object sender, MouseButtonEventArgs e)
  {
    ((UIElement) this.FixedDistanceFromStartTxNumericEditBox).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
  }

  private void DistanceFromStart_GotFocus(object sender, RoutedEventArgs e)
  {
    CApCableUtility.UnHighlightObject((ITxDisplayableObject) (this.AttachmentViewModel.FixedFrame as TxFrame));
    this.AttachmentViewModel.DistanceFromStartSelected = true;
  }

  private void DistanceFromStart_LostFocus(object sender, RoutedEventArgs e)
  {
    this.AttachmentViewModel.DistanceFromStartSelected = false;
  }

  private void FixedDistanceFromStartTxNumericEditBox_TentativeValueChanged(
    object sender,
    TxNumericEditBoxCtrl_TentativeValueChangedEventArgs args)
  {
    if (this.IsControlLoading || !this.IsControlActivated)
      return;
    this.AttachmentViewModel.SetLengthFromStartNoEventSent(args.TentativeValue * TxApplication.Options.Units.LinearMultiplier);
    this.ParameterChanged();
  }

  private void AttachmentFrameEditBoxCtrl_Picked(
    object sender,
    TxObjEditBoxCtrl_PickedEventArgs args)
  {
    TxFrame selectedFrame = this.FixedAttachmentFrameBoxCtrl.Object as TxFrame;
    ITxLocatableObject fixedFrame = this.AttachmentViewModel.FixedFrame;
    this.AttachmentFrameController.FrameEditorPicked(selectedFrame, ref fixedFrame, this.FixedAttachmentFrameBoxCtrl.Text);
    this.AttachmentViewModel.FixedFrame = fixedFrame;
  }

  private void FixedAttachmentFrame_KeyUp(object sender, KeyEventArgs e)
  {
    if (e.Key != Key.Back && e.Key != Key.Delete)
      return;
    this.AttachmentFrameController.AttachmentParentFrameDidChanged(this.AttachmentViewModel.FixedFrame);
    CApCableUtility.UnHighlightObject((ITxDisplayableObject) (this.AttachmentViewModel.FixedFrame as TxFrame));
    this.AttachmentViewModel.FixedFrame = (ITxLocatableObject) null;
    this.FixedAttachmentFrameBoxCtrl.Text = "";
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/flexibles/attachmentcontrols/capcablefixedattachmentcontrol.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.FixedAttachmentBorder = (Border) target;
        break;
      case 2:
        this.CanvasFixedAttachment = (Canvas) target;
        break;
      case 3:
        this.CableSimulatedSectionFixed = (Path) target;
        break;
      case 4:
        this.EndFrameAxesFixed = (Canvas) target;
        break;
      case 5:
        this.EndFramePointerFixed = (Canvas) target;
        break;
      case 6:
        this.EndFrameLabelFixed = (TextBlock) target;
        break;
      case 7:
        this.StartFrameAxesFixed = (Canvas) target;
        break;
      case 8:
        this.StartFramePointerFixed = (Canvas) target;
        break;
      case 9:
        this.StartFrameLabelFixed = (TextBlock) target;
        break;
      case 10:
        this.AttachmentFrameAxesFixed = (Canvas) target;
        break;
      case 11:
        this.AttachmentFramePointerFixed = (Canvas) target;
        break;
      case 12:
        this.AttachmentFrameLabelFixed = (TextBlock) target;
        break;
      case 13:
        this.CableNotSimulatedSectionFixed = (Path) target;
        break;
      case 14:
        this.DistanceFromStartFocusFixed = (Path) target;
        break;
      case 15:
        this.DistanceFromStartPathFixed = (Path) target;
        break;
      case 16 /*0x10*/:
        this.DistanceFromStartPointerFixed = (Canvas) target;
        break;
      case 17:
        this.DistanceFromStartLabelFixed = (TextBlock) target;
        break;
      case 18:
        this.AttachmentGraphicObjectFixed = (Rectangle) target;
        break;
      case 19:
        this.DistanceFromStartHitBoxFixed = (Canvas) target;
        this.DistanceFromStartHitBoxFixed.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.DistanceFromStart_DiagramFocus);
        break;
      case 20:
        this.AttachmentFrameHitBoxFixed = (Canvas) target;
        this.AttachmentFrameHitBoxFixed.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.AttachmentFrame_DiagramFocus);
        break;
      case 21:
        this.FixedAttachmentPanel = (StackPanel) target;
        break;
      case 22:
        this.FixedAttachmentGrid = (Grid) target;
        break;
      case 23:
        this.FixedAttachmentFrameLabel = (TextBlock) target;
        break;
      case 24:
        this.FixedAttachmentFrameBoxCtrl = (TxObjEditBoxControl) target;
        ((UIElement) this.FixedAttachmentFrameBoxCtrl).GotFocus += new RoutedEventHandler(this.AttachmentFrame_GotFocus);
        ((UIElement) this.FixedAttachmentFrameBoxCtrl).LostFocus += new RoutedEventHandler(this.AttachmentFrame_LostFocus);
        break;
      case 25:
        this.FixedDistanceFromStartLabel = (TextBlock) target;
        break;
      case 26:
        this.FixedDistanceFromStartTxNumericEditBox = (TxNumericEditBoxControl) target;
        ((UIElement) this.FixedDistanceFromStartTxNumericEditBox).GotFocus += new RoutedEventHandler(this.DistanceFromStart_GotFocus);
        ((UIElement) this.FixedDistanceFromStartTxNumericEditBox).LostFocus += new RoutedEventHandler(this.DistanceFromStart_LostFocus);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
